package com.example.spring.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.spring.entity.*;
import com.example.spring.entity.Record;
import com.example.spring.mapper.RecordMapper;
import com.example.spring.service.RecordService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private RecordService recordService;

    //新增或修改
    @PostMapping()
    public Integer save(@RequestBody Record record) {
        //新增或者更新
        return recordService.save(record);
    }
    //查询所有数据
    @GetMapping()
    public List<Record> findAll() {
        List<Record> all =  recordMapper.findAll();
        System.out.println(all);
        return all;
    }
    //删除
    @DeleteMapping("/{sid}")
    public Integer delete(@PathVariable Integer sid)
    {
        return recordMapper.deleteById(sid);
    }


    // 分页查询
    //  接口路径：/worker/page?pageNum=1&pageSize=10
    // @RequestParam接受
    //  limit第一个参数 = (pageNum - 1) * pageSize
    // pageSize
    @GetMapping("/page")
    public Map<String, Object> find_all_Page(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        List<Record> data = recordMapper.find_all_Page(pageNum, pageSize);
        Integer total = recordMapper.find_all_Total();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    //按照属性查询（无翻页）
    @PostMapping("/time")
    public List<Record> find_by_time(@RequestParam Timestamp maxTime,
                                @RequestParam Timestamp minTime){
        return recordMapper.findByTime(maxTime, minTime);
    }
    @PostMapping("/slot")
    public List<Record> find_by_slot(@RequestParam Slot slot){
        return recordMapper.findBySlot(slot.getSid());
    }
    @PostMapping("/type")
    public List<Record> find_by_time(@RequestParam Good good){
        return recordMapper.findByType(good.getType());
    }
    //按照属性查询（有翻页）
    @PostMapping("/time/Page")
    public Map<String, Object> find_by_time_page(@RequestParam Timestamp maxTime,
                                          @RequestParam Timestamp minTime,
                                          @RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<Record>data = recordMapper.findByTime_page(maxTime,minTime, pageNum, pageSize);
        Integer total = recordMapper.findByTime_Total(maxTime, minTime);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    @PostMapping("/place/Page")
    public Map<String, Object> find_by_slot_page(@RequestParam Integer sid,
                                                 @RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<Record> data = recordMapper.findBySlot_page(sid, pageNum, pageSize);
        Integer total = recordMapper.findBySlot_Total(sid);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    @PostMapping("/type/Page")
    public Map<String, Object> find_by_type_page(@RequestParam String type,
                                                 @RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<Record> data =  recordMapper.findByType_page(type, pageNum, pageSize);
        Integer total = recordMapper.findByType_Total(type);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    @PostMapping("/select/Page")
    public Map<String, Object> select_page(@RequestBody Attribute attribute,
                                           @RequestParam Integer pageNum,
                                           @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        String str = " AND whid = " + attribute.getWhid();
        boolean first = true;
        if (attribute.getMaxTime() != null){
            str = str + " AND r.create_time < '"  + attribute.getMaxTime() + "'";
        }
        if (attribute.getMinTime() != null){
            str = str + " AND r.create_time > '"  + attribute.getMinTime() + "'";
        }
        if (attribute.getSid() != null){
            str = str + " AND r.sid = "  + attribute.getSid();
        }
        if (attribute.getType() != null && attribute.getType() != ""){
            str = str + " AND g.type = '"  + attribute.getType() + "'";
        }
        List<Record> data =  recordMapper.select_page(str, pageNum, pageSize);
        Integer total = recordMapper.select_Total(str);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }


    //以下为数据统计部分
    @PostMapping("/data/total")
    public Map<String, Object>total_analyze(@RequestParam Integer whid, @RequestParam Timestamp minTime)
    {
        return recordService.total_analyze(whid, minTime);
    }

    @PostMapping("/data/total/date")
    public Map<String, Object>total_analyze_date(@RequestParam Integer whid,
                                                 @RequestParam Timestamp minTime,
                                                 @RequestParam Timestamp maxTime)
    {
        Map<String, Object> res = new HashMap<>();
        try {
            res = recordService.total_analyze_date(whid, minTime, maxTime);
        }
        catch(ParseException e)
        {

        }
        return res;
    }
    @PostMapping("/data/total/hour")
    public Map<String, Object>total_analyze_hour(@RequestParam Integer whid,
                                                 @RequestParam Timestamp minTime,
                                                 @RequestParam Timestamp maxTime)
    {
        Map<String, Object> res = new HashMap<>();
        try{
            res = recordService.total_analyze_hour(whid, minTime, maxTime);
        }
        catch(ParseException e)
        {

        }
        return res;
    }
    @PostMapping("/data/type/date")
    public Map<String, Object>type_analyze_date(@RequestParam Integer whid,
                                                 @RequestParam String type,
                                                 @RequestParam Timestamp minTime,
                                                 @RequestParam Timestamp maxTime)
    {
        Map<String, Object> res = new HashMap<>();
        try {
            res = recordService.type_analyze_date(whid, type, minTime, maxTime);
        }
        catch(ParseException e)
        {

        }
        return res;
    }
    @PostMapping("/data/type/hour")
    public Map<String, Object>type_analyze_hour(@RequestParam Integer whid,
                                                 @RequestParam String type,
                                                 @RequestParam Timestamp minTime,
                                                 @RequestParam Timestamp maxTime)
    {
        Map<String, Object> res = new HashMap<>();
        try{
            res=recordService.type_analyze_hour(whid, type, minTime, maxTime);
        }
        catch(ParseException e)
        {

        }
        return res;
    }

    @PostMapping("/data/slot/date")
    public Map<String, Object>slot_analyze_date(@RequestParam Integer sid,
                                                @RequestParam Timestamp minTime,
                                                @RequestParam Timestamp maxTime)
    {
        Map<String, Object> res = new HashMap<>();
        try {
            res = recordService.slot_analyze_date(sid, minTime, maxTime);
        }
        catch(ParseException e){

        }
        return res;
    }



    @PostMapping("/data/slot/hour")
    public Map<String, Object>slot_analyze_hour(@RequestParam Integer sid,
                                                @RequestParam Timestamp minTime,
                                                @RequestParam Timestamp maxTime)
    {
        Map<String, Object> res = new HashMap<>();
        try {
            res = recordService.slot_analyze_hour(sid, minTime, maxTime);
        }
        catch (ParseException e)
        {

        }
        return res;
    }


    @PostMapping("/data/type/date/all")
    public Map<String, Object>type_analyze_date_all(@RequestParam Integer whid,
                                                     @RequestParam Timestamp minTime,
                                                     @RequestParam Timestamp maxTime)
    {
        return recordService.type_analyze_date_all(whid, minTime, maxTime);
    }

    @PostMapping("/data/type/hour/all")
    public Map<String, Object>type_analyze_hour_all(@RequestParam Integer whid,
                                                    @RequestParam Timestamp minTime,
                                                    @RequestParam Timestamp maxTime)
    {
        return recordService.type_analyze_hour_all(whid, minTime, maxTime);
    }



    //将warehouse的全部slot的一段时间内的数量变化情况全部上传
    @PostMapping("/data/slot/date/all")
    public Map<Integer, Object>slot_analyze_date_all(@RequestParam Integer whid,
                                              @RequestParam Timestamp minTime,
                                              @RequestParam Timestamp maxTime)
    {
        return recordService.slot_analyze_date_all(whid, minTime, maxTime);
    }
    @PostMapping("/data/slot/hour/all")
    public Map<Integer, Object>slot_analyze_hour_all(@RequestParam Integer whid,
                                                     @RequestParam Timestamp minTime,
                                                     @RequestParam Timestamp maxTime)
    {
        return recordService.slot_analyze_hour_all(whid, minTime, maxTime);
    }


    @PostMapping("/data/proportion/type")
    public Map<String, Integer>type_proportion_date(@RequestParam Integer whid,
                                                    @RequestParam Timestamp time)
    {
        return recordService.type_proportion_wh(whid, time);
    }

}
